﻿using System;
using System.ServiceProcess;
using SledgeHammer;
using System.Threading;
using System.IO;

namespace AutoWump
{
    public partial class WumpingService : ServiceBase
    {
        //intiialize to 1 hour
        int intervalMinutes = 60;

        //Create a new timer object
        Timer tmrWumper; 

        public WumpingService()
        {
            InitializeComponent();

            //initialize the timer object
            tmrWumper = new Timer(new TimerCallback(this.WumpSites));

            //load a new Wumper instance and have it sync the
            //settings if there are new ones entered from the Wump Console
            Wumper wumper = new Wumper(true);
            intervalMinutes = wumper.IntervalMinutes;
        }

        public void TestStart()
        {
            string[] args = {""};
            OnStart(args);
        }

        protected override void OnStart(string[] args)
        {
            //add an entry to the log showing when the service started
            Wumper.LogServiceStartStop(true);

            //test the sites
            WumpSites(new Object());

            //start the timer
            tmrWumper.Change(0, intervalMinutes * 60000);
        }

        protected override void OnStop()
        {
            //add an entry to the log showing when the service stopped
            Wumper.LogServiceStartStop(false);
        }

        private void WumpSites(Object stateInfo)
        {
            //create a new wumper
            Wumper wumper = new Wumper();

            //test the sites
            wumper.TestAllSites();   
        }
    }
}
